package com.lsy.android.motu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ImageActivity extends Activity {

	private static final String IMAGE_URL = "http://www.diyi3d.com/3dmotu/index.html";
	private WebView mWebContent;
	private ProgressDialog loadingProgressDialog = null;
	private boolean mReload = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_content);
		loadingProgressDialog = new ProgressDialog(this);
		loadingProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		loadingProgressDialog.setMessage("loading...");
		loadingProgressDialog.setMax(100);

		mWebContent = (WebView) findViewById(R.id.web_content);
		mWebContent.setWebViewClient(new WebViewClient() {

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				mWebContent.reload();
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Intent intent = new Intent(ImageActivity.this,
						DetailActivity.class);
				intent.putExtra("url", url);
				startActivity(intent);
				return true;
			}
		});
		mWebContent.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				if (newProgress >= 100) {
					if (!mReload) {
						mReload = true;
						new Handler(getMainLooper()).postDelayed(
								new Runnable() {
									@Override
									public void run() {
										mWebContent.loadUrl(IMAGE_URL);
									}
								}, 100);
					}
				}
			}
		});
		mWebContent.loadUrl(IMAGE_URL);
	}

}
